package cn.wei.sdd.rabbitutils.config;

import cn.wei.sdd.rabbitutils.constant.MQConst;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wei
 * @Date: 2022/01/24/15:27
 */
@Configuration
public class MQConfig {
    @Bean
    public MessageConverter messageConverter(){
//        Json字符串转换器
        return new Jackson2JsonMessageConverter();
    }

    //声明一个交换机、1个路由、一个队列

    //声明延迟交换机
    @Bean("delayExchange")
    public CustomExchange delayExchange(){
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange(MQConst.DELAY_EXCHANGE_ORDER,"x-delayed-message",true,false,args);
    }

    //声明延迟队列
    @Bean("delayQueue")
    public Queue delayQueue(){
        return new Queue(MQConst.DELAY_QUEUE_ORDER);
    }

    //声明延迟队列的绑定关系
    @Bean
    public Binding delayBinding(@Qualifier("delayQueue") Queue queue,
                                @Qualifier("delayExchange") CustomExchange exchange
                                ){
        return BindingBuilder.bind(queue).to(exchange).with(MQConst.DELAY_ROUTING_ORDER).noargs();
    }

}
